<?php
/**
 * 新闻动态
 * Created by PhpStorm.
 * User: xsh
 * Date: 2018/4/12  10:06
 */
namespace app\mobile\controller;
use think\Controller;

class Dynamic extends Base
{
    /*
    *	构造函数
    */
    function __construct(){
        parent::__construct();
    }

    public function zindex()
    {
        $dynamic = db('dynamic');

        $dynamic_category_result = db('dynamic_category')->where(array('category_name'=>'高企专栏'))->field('id')->select();
        
        $dynamic_category_result = array_column($dynamic_category_result, 'id');
        $where['category'] = array('in', $dynamic_category_result);
        $where['is_display'] = 1;

        $data = $dynamic->field('id,title,create_time')->where($where)->limit($this->per_page)->order('order_by','desc')->select();

        foreach ($data as $key => &$value) {
            $value['create_time'] = date("Y-m-d",$value['create_time']);
        }

        $dynamic_count = $dynamic->where($where)->count();

        if($dynamic_count > $this->per_page){
            $click_for_more=true;
        }else{
            $click_for_more=false;
        }

        $this->assign('data', $data);
        $this->assign('click_for_more', $click_for_more);
        return $this->fetch();
    }

    public function ajax_page(){
        $page = input("page");

        $start_page = ($page - 1)*$this->per_page;

        $dynamic_category_result = db('dynamic_category')->where(array('category_name'=>'高企专栏'))->field('id')->select();
        
        $dynamic_category_result = array_column($dynamic_category_result, 'id');
        $where['category'] = array('in', $dynamic_category_result);
        $where['is_display'] = 1;

        $dynamic = db('dynamic')->where($where)->limit("$start_page,$this->per_page")->order('id desc')->select();

        foreach ($dynamic as $key => &$value) {
            $value['create_time'] = date("Y-m-d",$value['create_time']);
            $value['url'] = url('Dynamic/detail',array('id'=>$value['id']));
        }

        $this->success($dynamic);
    }

    //详情
    public function detail(){
        $id = input('id');

        $data = db('dynamic')
            ->where('id',$id)
            ->find();
        
        $this->assign('data', $data);
        return $this->fetch('Dynamic/zdetail');//加载iframe
    }

}